package pro.cherish.knowledgeumldesignpattern.designPattern.com.cherish.composite;

import java.util.ArrayList;
import java.util.List;

//抽像构建
public interface AbstractFile {
//    杀毒
    void killVirus();
}

class ImageFile implements AbstractFile{
    private String name;
    public ImageFile(String name) {
        this.name = name;
    }
    @Override
    public void killVirus() {
        System.out.println(name+"图像文件查杀");
    }
}

class TextFile implements AbstractFile{
    private String name;
    public TextFile(String name) {
        this.name = name;
    }
    @Override
    public void killVirus() {
        System.out.println(name+"文本文件查杀");
    }
}

class VideoFile implements AbstractFile{
    private String name;
    public VideoFile(String name) {
        this.name = name;
    }
    @Override
    public void killVirus() {
        System.out.println(name+"视频文件查杀");
    }
}

//容器组件
class Folder implements AbstractFile{
    private String name;
//    定义容器，用来存放容器组件下存放的子节点
    private List<AbstractFile> list=new ArrayList<AbstractFile>();

    public Folder(String name) {
        this.name = name;
    }

    public void add(AbstractFile file){
        list.add(file);
    }
    public void remove(AbstractFile file){
        list.remove(file);
    }
    public AbstractFile getChild(int index){
        return list.get(index);
    }
    @Override
    public void killVirus() {
        System.out.println(name+"文件夹进行查杀");
        for (AbstractFile file:list){
            file.killVirus();
        }
    }

}